﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetfractionHub.Network.Protocol;
using NetfractionHub.Network.Protocol.BASE.Factory;

namespace NetfractionHub.LocalMachine.BotCommands.Handler
{
    [CommandHandler("listpermissions", TriggerAlias="listperm")]
    public class ListPermissions : CommandHandler
    {
        public override BotReturnStatus HandleCommandMessage(NetfractionHub.RemoteMachine.Node source, CommandMessage message)
        {
            if (source == null)
                return new BotReturnStatus(false);

            if (message.BooleanArguments.Count == 0 && message.StringArguments.Count == 0)
                return new BotReturnStatus(false, "No arguments supplied. Use --all, --my, or -user.");

            if (message.BooleanArguments.ContainsKey("all"))
                if (Security.Permission.HasPermission(source.SecurityIdentity, typeof(Security.Permissions.ListAllPermissionsPermission)))
                    return new BotReturnStatus(true, Security.Permission.ListAllPermissions());
                else
                    return new BotReturnStatus(false, Security.Permission.GetDeniedString(typeof(Security.Permissions.ListAllPermissionsPermission)));

            if (message.BooleanArguments.ContainsKey("my"))
                if (Security.Permission.HasPermission(source.SecurityIdentity, typeof(Security.Permissions.ListOwnIdentityPermissionsPermission)))
                    return new BotReturnStatus(true, source.SecurityIdentity.ListPermissions());
                else
                    return new BotReturnStatus(false, Security.Permission.GetDeniedString(typeof(Security.Permissions.ListOwnIdentityPermissionsPermission)));

            if (message.StringArguments.ContainsKey("user"))
            {
                RemoteMachine.Node user = Network.ConnectionManager.GetConnectedNodeByNickname(message.StringArguments["user"]);
                if (user == null)
                    return new BotReturnStatus(false, string.Format("Cannot list permissions, the user '{0}' does not exist.", message.StringArguments["user"]));

                if (!Security.Permission.HasPermission(source.SecurityIdentity, typeof(Security.Permissions.ListIdentityPermissionsPermission)))
                    this.ParentBot.SendEchoMessageFromBot(source, Security.Permission.GetDeniedString(typeof(Security.Permissions.ListIdentityPermissionsPermission)));

                if (!Security.RoleManager.IsInAncestorTree(source.SecurityIdentity.Role, user.SecurityIdentity.Role))
                    return new BotReturnStatus(false, string.Format("Permission denied. You are not an ancestor of '{0}'.", message.StringArguments["user"]));

                return new BotReturnStatus(true, user.SecurityIdentity.ListPermissions());
            }

            if (message.StringArguments.ContainsKey("role"))
            {
                Security.Role roleRequested = Security.RoleManager.GetRoleByName(message.StringArguments["role"]);
                if (roleRequested == null)
                    return new BotReturnStatus(false, string.Format("Cannot list permissions, the role '{0}' does not exist.", message.StringArguments["role"]));

                if (!Security.Permission.HasPermission(source.SecurityIdentity, typeof(Security.Permissions.ListIdentityPermissionsPermission)))
                    return new BotReturnStatus(false, Security.Permission.GetDeniedString(typeof(Security.Permissions.ListIdentityPermissionsPermission)));

                if (!Security.RoleManager.IsInAncestorTree(source.SecurityIdentity.Role, roleRequested))
                    return new BotReturnStatus(false, string.Format("Permission denied. You are not an ancestor of '{0}'.", message.StringArguments["role"]));

                return new BotReturnStatus(true, roleRequested.ListPermissions());
            }

            return false;
        }
    }
}
